﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace RaidAnything.FS
{
    /// <summary>
    /// Uniquely identifies a block
    /// </summary>
    public class BlockId
    {
        private byte[] _unique = new byte[0];

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(_unique != null);
        }

        /// <summary>
        /// Returns the unique byte array which identifies a block
        /// </summary>
        public byte[] Unique
        {
            get { return _unique; }
            set { Contract.Requires(value != null);
            _unique = value;
            }
        }

        /// <summary>
        /// returns true if two blockid's are equivalent
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is BlockId))
            {
                return false;
            }
            //check fields
            return base.Equals(obj);
        }

        /// <summary>
        /// Returns the hashcode for this block id
        /// </summary>
        /// <returns>Returns the hashcode for this block id</returns>
        public override int GetHashCode()
        {
            return (int)GetLongHashCode();
        }
        /// <summary>
        /// Calculates the Adler32Checsum for the unique byte array identifier
        /// </summary>
        /// <returns> Calculates the Adler32Checsum for the unique byte identifier</returns>
        public long GetLongHashCode()
        {
            return Adler32Checksum.Adler32.Calculate(Unique);
        }
    }
}
